package com.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.core.baseDao.extend.Pager;
import com.manager.KeywordManager;
import com.model.Keyword;
import com.model.ModuleConstant;
import com.opensymphony.xwork2.ActionSupport;
import com.util.ServletUtils;
import com.util.TimeUtil;


@ParentPackage("json-default")
@Results({ @Result(name = "jsonResult", type = "json") })
@Namespace("/user")
public class LotteryAction extends ActionSupport{
	
//	private static final String LOTTERY = "LOTTERY";
//	private static final String 
	
	private static final long serialVersionUID = 1L;
	
	private Keyword keyword;
	@Autowired
	private KeywordManager keywordManager;
	
	public String add(){
		return decide()+"-add";
	}
	
	public String decide(){
		ServletRequest request = ServletActionContext.getRequest();
		String type = request.getParameter("lottery");
		if(StringUtils.isEmpty(type)) return "";
		if(type.equals(ModuleConstant.LOTTERY_LOTERY)){
			return "lottery";
		}else if(type.equals(ModuleConstant.LOTTERY_COUPON)){
			return "coupon";
		}else{
			return "guajiang";
		}
	}
	
	public String changeStatus(){//0未开始 1开始 2结束 
		ServletRequest request = ServletActionContext.getRequest();
		String status = request.getParameter("status");
		String id = request.getParameter("id");
		if(StringUtils.isEmpty(status) || StringUtils.isEmpty(id)) return index();
		int stat = Integer.parseInt(status);
		try {
			keyword = (Keyword)keywordManager.findUnique("from Keyword k where k.id=?", 
					new Object[]{Integer.parseInt(id)});
			keyword.getLottery().setStatus(stat);
			keywordManager.update(keyword);
		} catch (Exception e) {
			request.setAttribute("ret", "数据库服务器出错，请联系开发人员");
			return index();
		}
		request.setAttribute("ret", "更改活动状态成功");
		return index();
	}
	
	public String addTo(){
		ServletRequest request = ServletActionContext.getRequest();
		String token = ServletUtils.getToken();
		String model = request.getParameter("lottery");;
		String ret = "";
		keyword.setToken(token);
		keyword.getLottery().setToken(token);
		if(keyword.getId() == null){
			ret = "添加成功！";
			keyword.getLottery().setCreateTime(TimeUtil.getDate());
			keyword.getLottery().setUpdateTime(TimeUtil.getDate());
			keyword.getLottery().setFistLuckNums(keyword.getLottery().getFistNums());
			keyword.getLottery().setSecondLuckNums(keyword.getLottery().getSecondNums());
			keyword.getLottery().setThirdLuckNums(keyword.getLottery().getThirdNums());
			keyword.getLottery().setFourLuckNums(keyword.getLottery().getFourNums());
			keyword.getLottery().setFiveLuckNums(keyword.getLottery().getFiveNums());
			keyword.getLottery().setStatus(0);
			keyword.getLottery().setClick(0);
			
		}else{
			ret = "修改成功！";
			Keyword key = (Keyword)keywordManager.findUnique("from Keyword k where k.id=?", 
					new Object[]{keyword.getId()});
			keyword.getLottery().setCreateTime(key.getLottery().getCreateTime());
			keyword.getLottery().setFistLuckNums(key.getLottery().getFistLuckNums());
			keyword.getLottery().setSecondLuckNums(key.getLottery().getSecondLuckNums());
			keyword.getLottery().setThirdLuckNums(key.getLottery().getThirdLuckNums());
			keyword.getLottery().setFourLuckNums(key.getLottery().getFourLuckNums());
			keyword.getLottery().setFiveLuckNums(key.getLottery().getFiveLuckNums());
			keyword.setModel(key.getModel());
			keyword.getLottery().setStatus(key.getLottery().getStatus());
			keyword.setType(key.getType());
			keyword.setToken(key.getToken());
			keyword.getLottery().setClick(key.getLottery().getClick());
		}
		keyword.getLottery().setType(model);
		keyword.getLottery().setKeyword(keyword);
		try {
			keywordManager.saveOrUpdate(keyword);
		} catch (Exception e) {
			request.setAttribute("ret", "服务器内部错误，请于工作人员联系！");
			return index();
		}
		request.setAttribute("ret", ret);
		return index();
	}
	
	public String edit(){
		ServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		if(StringUtils.isEmpty(id))return index();
		keyword = (Keyword)keywordManager.findUnique("from Keyword k where k.id=?", 
				new Object[]{Integer.parseInt(id)});
		return decide()+"-edit";
	}
	
	public String delete(){
		ServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		if(StringUtils.isEmpty(id))return index();
		keyword = (Keyword)keywordManager.findUnique("from Keyword k where k.id=?", 
				new Object[]{Integer.parseInt(id)});
		try {
			keywordManager.delete(keyword);
			request.setAttribute("ret", "删除成功");
		} catch (Exception e) {
			request.setAttribute("ret", "数据库出错，请于工作员联系！");
		}
		return index();
	}
	
	public String index(){
		ServletRequest request = ServletActionContext.getRequest();
		Pager page = new Pager();
		String p = request.getParameter("p");
		if (!StringUtils.isEmpty(p)) {
			page.setCurrPage(Integer.parseInt(p));
		}
		String model = request.getParameter("lottery");
		// Map session = ActionContext.getContext().getSession();
		// String token = (String)session.get("token");
		Map<String, Object> params = new HashMap<String, Object>();
		// params.put("token", token);
		params.put("model", model);
		page = keywordManager.findByPage("from Keyword ", params, page);
		request.setAttribute("page", page);
		return decide()+"-index";
	}

	public Keyword getKeyword() {
		return keyword; 
	}

	public KeywordManager getKeywordManager() {
		return keywordManager;
	}

	public void setKeyword(Keyword keyword) {
		this.keyword = keyword;
	}

	public void setKeywordManager(KeywordManager keywordManager) {
		this.keywordManager = keywordManager;
	}
}
